<html>
<head>
<title>array test - no framework</title>
<script type="text/javascript">
function cl( e){
	console.log(e);
}
function clj( e){
	console.log(JSON.stringify(e));
}
function json( e){
	return JSON.stringify(e);
}

/* mine */
function makeUnique( arr){
	arr = arr.sort();
	var arr2 = new Array();
	for( i =0; i< arr.length; i++){
		arr2.push( arr[i]);
		i = arr.lastIndexOf(arr[i]);
	}
	return arr2;
}

function makeUnique2( arr){
	arr = arr.sort();
	var arr2 = new Array();
	for( i =arr.length-1; i>=0; --i){
		arr2.push( arr[i]);
		i = arr.indexOf(arr[i]);
	}
	return arr2;
}

function makeUnique3( arr){
	arr = arr.sort();
	var arr2 = new Array();
	for( i =0; i< arr.length; i++){
		arr2.push( arr[i]);
		i = arr.indexOf(arr[i],i+1);
	}
	return arr2;
}

function makeUnique4( arr,key){
	arr = arr.sort(function sortfn(elem0,elem1){
		return elem0[key] < elem1[key]? -1 : ( elem0[key] > elem1[key]? 1 : 0 );
		var lastA = elem0[key];
		var lastB = elem1[key];
		if (lastA < lastB) return -1;
		if (lastA > lastB) return 1;
		if (lastA == lastB) return 0;
	});
	var arr2 = new Array();
	for( i =arr.length-1; i>=0; --i){
		arr2.push( arr[i]);
		i = arr.indexOf(arr[i],i);
	}
	return arr2;
}

function makeUnique5( arr){
	var arr2 = new Array();
	for( i =0; i< arr.length; i++){
		if( !~arr.indexOf(arr[i],i+1))
			arr2.push( arr[i]);
	}
	return arr2;
}

function makeUnique6( arr){
	var arr2 = new Array();
	for( var i =arr.length; i>=0; i--){
		if( !~arr.lastIndexOf(arr[i],i-1))
			arr2.push( arr[i]);
	}
	return arr2;
}

function makeUnique7( arr){
	var arr2 = new Array();
	for( i =0; i< arr.length; i++){
		if( !~arr2.indexOf(arr[i]))
			arr2.push( arr[i]);
	}
	return arr2;
}

function obj(new_id){
	this.id = new_id;
	this.data = Math.random();
}

function test(){

	var arr = new Array();
	for( var i=1000; i>0;--i)
		arr.push( (i%900).toString());
	var arrObj = new Array();
	for( var i=100; i>0;--i)
		arrObj.push( new obj((i%50).toString()));
		
	console.profile();
	arr = arr.sort();
	for( var i = 20; i>0; --i){
		var arr1 = makeUnique( arr);
		var arr2 = makeUnique2( arr);
		//var arr3 = makeUnique3( arr);
		var arr4 = makeUnique5( arr);
		var arr5 = makeUnique6( arr);
		var arr7 = makeUnique7( arr);
	}
	console.profileEnd();
	
	/*console.profile();
	var brr1 = makeUnique(arrObj);
	var brr5 = makeUnique2(arrObj);
	var brr6 = makeUnique3(arrObj);
	var brr4 = makeUnique4(arrObj,"id");
	console.profileEnd();*/
	
	cl( arr1.length);
	cl( arr2.length);
	//cl( arr3.length);
	cl( arr4.length);
	cl( arr5.length);
	cl( arr7.length);

}
</script>
</head>
<body>
<a href="javascript:test()">RUN TEST</a>
</body>
</html>
